// constSpringSet.cpp: implementation of the ConstSpringSet class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Phsshare.h"
#include "constSpringSet.h"
#include "PersistentFacade.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

using namespace DAWrapper;
ConstSpringSet::ConstSpringSet()
{

}

ConstSpringSet::~ConstSpringSet()
{

}

BOOL ConstSpringSet::GetConstSpringSet(DOUBLE dbDisplacement, DOUBLE dbLoad)
{
	ASSERT(!this->GetDataStandard().IsEmpty());
	ConstSpringDisSerial* pSerial = GetPersistentFacade().GetConstSpringDisSerialManager()
		->GetConstSpringDisSerial(this->GetDataStandard());
	ASSERT(pSerial);

	DOUBLE dbDis = pSerial->GetFirstDis(dbDisplacement);

	DOUBLE dbCapacity = dbDis*dbLoad/1000;

	return GetConstSpringSet(dbCapacity);
}

BOOL ConstSpringSet::GetConstSpringSet(DOUBLE dbCapacity)
{
	CString strSql;
	strSql.Format(_T("select * from %s order by abs(capacity - %g)"),
		GetPropertyTable(), dbCapacity);

	Close();

	try
	{
		_ConnectionPtr* pCon = DBConnectionManager::Instance()
			->GetConnectionPtrByUri(this->GetSourceURL());
		
		m_rs = (*pCon)->Execute(_bstr_t(strSql), NULL, adCmdText);
	}
	CATCH_COM_ERROR(m_rs, FALSE)

	return TRUE;
}

const CString& ConstSpringSet::GetPropertyTable()
{
	StandardOfConstSpring* pSpring = this->GetStandardOfConstSpring();
	ASSERT(pSpring);

	m_theTables.Add(pSpring->GetPropertyDataTable());
	return pSpring->GetPropertyDataTable();
}

ConstSpring* ConstSpringSet::GetFirstConstSpring()
{
	try
	{
		if(m_rs == NULL || (m_rs->adoEOF && m_rs->BOF))
			return NULL;
		
		m_rs->MoveFirst();
		return GetConstSpring();
	}
	CATCH_COM_ERROR(m_rs, NULL)
}

ConstSpring* ConstSpringSet::GetNextConstSpring()
{
	try
	{
		m_rs->MoveNext();
		if(m_rs == NULL || m_rs->adoEOF || m_rs->BOF)
		{		
			Close();
			return NULL;
		}
		
		return GetConstSpring();
	}
	CATCH_COM_ERROR(m_rs, NULL)
}

ConstSpring* ConstSpringSet::GetConstSpring()
{
	ConstSpring* pSpring = new ConstSpring;
	if(pSpring == NULL)
	{
		ReportMemoryError();
		return NULL;
	}
	
	pSpring->Capacity(vtof(m_rs->GetCollect(_T("Capacity"))));
	pSpring->DH(vtoi(m_rs->GetCollect(_T("DH"))));
	pSpring->CustomID(vtos(m_rs->GetCollect(_T("CustomID"))));
	pSpring->ManuFactory(vtos(m_rs->GetCollect(_T("ManuFactory"))));
	pSpring->MaxDis(vtof(m_rs->GetCollect(_T("MaxDist"))));
	pSpring->MinDis(vtof(m_rs->GetCollect(_T("MinDist"))));
	pSpring->MidDis(vtof(m_rs->GetCollect(_T("MidDist"))));

	return pSpring;
}
